package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.PackageStatusEnum;
import com.xbongbong.pro.enums.ValueAddedTypeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.service.aliyun.AliyunOssService;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.service.toolbox.help.PackageStatusHelp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;
import java.util.Objects;

/**
 * @author sunshaoxiang
 * @ClassName AliyunMobileController
 * @Description 阿里云相关controller
 * @Date 2022/8/26 14:40
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/aliyun")
public class AliyunMobileController {

    private static final Logger LOG = LoggerFactory.getLogger(AliyunMobileController.class);

    @Resource
    private PackageStatusHelp packageStatusHelp;
    @Resource
    private AliyunOssService aliyunOssService;

    /**
     * 通过sts鉴权获取阿里云OSS临时访问凭证，用于前端上传图片/文件
     * @return 签名信息
     */
    @RequestMapping(value = "/oss/sts/getSignature", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getStsSignature(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) throws Exception {
        XbbResponse<Map<String, Object>> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            String corpid = baseDTO.getCorpid();
            Integer status = packageStatusHelp.getPackageStatus(corpid, ValueAddedTypeEnum.CAPACITY_NUM, 0);
            // 上传文件用量前端处理，后端只传标识
            Integer overLimit = !Objects.equals(status, PackageStatusEnum.NORMAL.getCode()) && !Objects.equals(status, PackageStatusEnum.WARNING.getCode()) ? BasicConstant.ONE : BasicConstant.ZERO;
//            if(!Objects.equals(status, PackageStatusEnum.NORMAL.getCode()) && !Objects.equals(status, PackageStatusEnum.WARNING.getCode())){
//                throw new XbbException(ErrorCodeEnum.API_ERROR_100074, "存储量已达上限，可联系客服购买容量！");
//            }
            try {
                Map<String, Object> resultMap = aliyunOssService.getStsSignature(baseDTO);
                resultMap.put("overLimit", overLimit);
                response = new XbbResponse<>(resultMap);
            } catch (XbbException xe) {
                response = new XbbResponse<>(xe);
            } catch (Exception e) {
                LOG.error("计算签名时出错", e);
                response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100001, "获取签名相关参数出错");
            }
        }
        return JSON.toJSONString(response);
    }
}
